/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.AA中等题.分数到小数;

import java.util.HashMap;
import java.util.HashSet;
import java.util.ResourceBundle;

public class Main {
    public static void main(String arg[]) {//main--来自高金磊

        new Main().fractionToDecimal(-1, -2147483648);
        System.out.println(-0 >> 31);
    }

    public String fractionToDecimal(int numerator, int denominator) {
        StringBuilder res = new StringBuilder();
        if ((double) numerator / denominator < 0) {
            res.append('-');
        }
        long numerator_big = numerator;
        long denominator_big = denominator;
        numerator_big = Math.abs(numerator_big);
        denominator_big = Math.abs(denominator_big);
        HashMap<Long, Integer> data = new HashMap<>();
        //缓存计算结果

        res.append(String.valueOf((numerator_big / denominator_big)));
        numerator_big %= denominator_big;
        if (numerator_big != 0)
            res.append('.');
        int n = res.length();
        int pre = 0;
        while (numerator_big != 0) {
            if (data.getOrDefault(numerator_big, 0) != 0) {
                pre = data.get(numerator_big);
                break;
            }
            data.put(numerator_big, n);
            n++;
            numerator_big *= 10;
            res.append(numerator_big / denominator_big);
            numerator_big %= denominator_big;
        }
        if (pre == 0) {
            return res.toString();
        }
        res.insert(pre, '(');
        res.insert(n + 1, ')');
        return res.toString();
    }
}
